/*
 * TextQuery.h
 *
 * 1. 保存文本内容（按行）， file
 * 2. 保存单词与其出现行号的map映射
 *
 *  Created on: 2020年11月28日
 *      Author: san
 */

#ifndef TEXTQUERY_H_
#define TEXTQUERY_H_

#include <fstream>
#include <memory>
#include <map>
#include <vector>
#include <set>
#include <sstream>

using std::string;

class TextQuery {
public:
	class QueryResult;
	using line_no = std::vector<std::string>::size_type;
	TextQuery();
	TextQuery(std::ifstream &infile);
	virtual ~TextQuery();

	QueryResult query(const string &s) const;

private:
	std::shared_ptr<std::vector<std::string>> file;
	std::map<string, std::shared_ptr<std::set<line_no>>> word_lownum_map;
};

class TextQuery::QueryResult {
	friend std::ostream &print(std::ostream &out, const QueryResult &qr)
	{
		out << qr.sought << " occurs " << qr.lines->size() << "times" << std::endl;
		auto beg = qr.lines->cbegin();
		while (beg != qr.lines->cend())
		{
			out << "    " << "(line" << *beg << ")" <<
					qr.file->at(*beg) << std::endl;
			++beg;
		}
		return out;
	};
public:
//	using line_no = std::vector<std::string>::size_type;
	QueryResult() {};
	QueryResult(std::string s, std::shared_ptr<std::set<line_no>> l,
			std::shared_ptr<std::vector<std::string>> f): sought(s), lines(l), file(f){ }
	virtual ~QueryResult() {};

	auto begin() { return lines->begin(); }
	auto end() { return lines->end(); }
	auto get_file() { return file; }

private:
	std::string sought;
	std::shared_ptr<std::set<line_no>> lines;
	std::shared_ptr<std::vector<std::string>> file;
};

//TextQuery::QueryResult::QueryResult() {
//	// TODO Auto-generated constructor stub
//
//}

//std::ostream &print(std::ostream& out, const TextQuery::QueryResult &qr)




#endif /* TEXTQUERY_H_ */
